//----------------------------------------------------------------------------
// Filename                : isr.c
//
// Functional Description  : Interrupt Service Routines
//----------------------------------------------------------------------------

#include "DRV922xx_trapezoidal_sensored.h"
#include "message_protocol_application.h"

// Interrupt handlers

interrupt void measure_isr(void);										/*!< Called right before the mid-point and end of the PWM duty for sampling current, voltage and calling BEMF state machine */
//interrupt void ipd_align_timer_isr(void);								/*!< Called on timer tick (1ms) when the IPD and Align and Go state machines are to be called */
interrupt void invalid_int_isr(void);									/*!< Called for invalid interrupts for debugging */
interrupt void afe_over_current_isr(void);								/*!< Called when PORT1 interrupt received (h/w OC) */
interrupt void coarse_timer_tick(void);									/*!< Called at slow rate */
interrupt void hall_change_isr(void);

/*
 *  \fn void coarse_timer_tick
 *  \brief ISR for coarse timer tick
 * 
 *  This ISR fires at a coarse timer (1ms) and is used for flagging the PWM duty cycle to be updated
 */

#pragma vector = TIMER1_A0_VECTOR		
interrupt void coarse_timer_tick(void)
{	
	static int timer10ms = 0;
//	P1OUT |= BIT6;
	WDTCTL = WDTPW + WDTCNTCL; // CLEAR THE WATCH DOG COUNTER 
	process1msFlag = 1;
	sample1msADCsFlag = 1;
	if (++timer10ms == 10)
	{
		timer10ms = 0;
		process10msFlag = 1;
	}
	#if TEST_WITH_NO_CURRENT_SAMPLING
		sample_peak_current();
		sample_throttle();
	#endif
	
	TA1CCTL0 &= ~CCIFG;
//	P1OUT &= ~BIT6;
}
/*
 *  \fn void measure_isr(void)
 *  \brief Measurement ISR - called when time to measure analog signals
 */
#pragma vector = TIMER3_A1_VECTOR		 	// 45
interrupt void measure_isr(void)
{
	static unsigned int offset1msSample = 0;
//	if (TA3IV == TA3IV_TA1CCR1)				// TA1CCR1: PWM high mid-point
//	{
//		if (pwm_duty >= (CCR_MAX >> 1))		// If duty is more than 50%, then sample throttle here
//		{
//			if (sample1msADCsFlag == 1)
//			{
//				sample1msADCsFlag = 0;
//				sample_throttle();					
////			}
//		}
//		TA3CCTL1 &= ~CCIFG;					// Clear interrupt flag
//	}
//	if (TA3IV == TA3IV_TA1CCR2)				// TA1CCR2: PWM high end-point - 3us
//	{
	P1OUT |= BIT6;
//	P2OUT &= ~BIT0;
//		pwm_blink_led();
		pwmTicks++; // increment for speed calculation
		sample_peak_current();				// Sample peak current, perform firmware ILimit
//		sample_temperature();
//		if (pwm_duty < (CCR_MAX >> 1))		// If duty is less than 50%, then sample throttle here as well
//		{
			if (offset1msSample == 1)
			{
				offset1msSample = 0;
				sample_temperature();
			}
			if (sample1msADCsFlag == 1)
			{
				sample1msADCsFlag = 0;
				offset1msSample = 1;
				sample_throttle();					
			}
			
//		}
		TA3CCTL2 &= ~CCIFG;					// Clear interrupt flag
//		pwm_blink_led();
		P1OUT &= ~BIT6;
//		send_cmd(CmdStatus, 0x7799);
//		uart_tick_1ms();
//		P1OUT &= ~BIT6;
//	}
}		

/*
 *  \fn void afe_over_current_isr(void)
 *  \brief Handles hardware over-current
 */
#pragma vector = PORT1_VECTOR			// 60
interrupt void afe_over_current_isr(void)
{
	if (P1IV == P1IV_P1IFG1)
	{
		// Hardware over-current has occurred
		_disable_interrupts();
		AppStatus |= StatusHwOc;
		
		// Disable all PWMs
		pwm_disable();
		
		// Disable all timers	
		TA3CTL = MC__STOP;
		TBCTL = MC__STOP;
		
		// Blink P2.0 slowly (blocking function)
		blink_led(2);
	}
}
/*
 *  \fn void hall_change_isr(void)
 *  \brief Handles hall sensor interrupts
 */
#pragma vector = PORT3_VECTOR			// 58
interrupt void hall_change_isr(void)
{
	hallIsrFlag = 1;
	isMoving = IS_MOVING;
	P3IFG = 0;    						// Clear pending Hall interrupts
}
/*
 *  \fn void invalid_int_handler(void)
 *  \brief Define an interrupt vector for unused interrupts, to catch interrupt errors in programming
 */
#pragma vector = SYSNMI_VECTOR           // 62
#pragma vector = UNMI_VECTOR             // 61
//#pragma vector = PORT1_VECTOR            // 59
#pragma vector = PORT2_VECTOR            // 59
#pragma vector = TIMER0_A0_VECTOR        // 57
#pragma vector = TIMER0_A1_VECTOR        // 56
#pragma vector = TIMER1_A1_VECTOR        // 54
#pragma vector = TIMER0_B0_VECTOR        // 53
#pragma vector = TIMER0_B1_VECTOR        // 52
#pragma vector = USCI_B0_VECTOR          // 50
#pragma vector = TIMER2_A0_VECTOR        // 49
#pragma vector = TIMER2_A1_VECTOR        // 48
#pragma vector = DMA_VECTOR              // 47
#pragma vector = TIMER3_A0_VECTOR        // 46
#pragma vector = TIMER4_A0_VECTOR		 // 44
#pragma vector = TIMER4_A1_VECTOR		 // 43
#pragma vector = PORT4_VECTOR            // 42
//#pragma vector = PORT3_VECTOR            // 58
#pragma vector = WDT_VECTOR              // 41
//#pragma vector = RESET_VECTOR            // 63
interrupt void invalid_int_isr(void)
{
	while(1);
}
